/**
 * @class Spark.TriggerField
 * @extends Spark.TextField
 * <p>触发类组件.
 * @author njq.niu@hand-china.com
 * @constructor
 * @param {Object} config 配置对象. 
 */
$S.TriggerField = Ext.extend($S.TextField,{
    isExpanded : false,
	constructor: function(config) {
        $S.TriggerField.superclass.constructor.call(this, config);
    },
    initComponent : function(config){
        $S.TriggerField.superclass.initComponent.call(this, config);
        this.trigger = this.wrap.child('div[stype=trigger]');
        this.initPopup();
    },
    initPopup: function(){
    	if(this.initpopuped == true) return;
    	this.popup = this.wrap.child('div[stype=popup]');
        if(this.popup){
        	Ext.getBody().insertFirst(this.popup);
        	this.initpopuped = true;
        }
    },
    processListener: function(ou){
        $S.TriggerField.superclass.processListener.call(this, ou);
        this.trigger[ou]('click',this.onTriggerClick, this, {preventDefault:true});
        this.el[ou]("click",   this.onInputClick, this)
    },
    setHeight: function(h){
        $S.TriggerField.superclass.setHeight.call(this, h);
        this.trigger.setStyle("height",(h-2)+"px");
    },
    setWidth: function(w){
		this.wrap.setStyle("width",(w-2)+"px");
        this.el.setStyle("width",(w-28)+"px");
	},    
    /*onBlur : function(e){
//        if(this.isEventFromComponent(e.target)) return;
//    	if(!this.hasExpanded()){
	    	this.isFocused = false;
	        this.wrap.removeClass(this.focusCss);
	        this.fireEvent("blur", this);
//    	}
    },*/    
    isEventFromComponent:function(el){
    	return $S.TriggerField.superclass.isEventFromComponent.call(this,el)|| (this.popup && (this.popup.dom == el || this.popup.contains(el)));
    },
	destroy : function(){
        $S.TriggerField.superclass.destroy.call(this);
        this.collapse();
        if(this.popup)
    	this.popup.remove();
	},
    triggerBlur : function(e,t){
    	if(!this.isEventFromComponent(t)){    
    		this.collapse();
        }
    },
    setVisible : function(v){
    	$S.TriggerField.superclass.setVisible.call(this,v);
    	if(v == false){
    		this.collapse();
    	}
    },
    /**
     * 折叠弹出面板
     */
    collapse : function(){
        if(!this.isExpanded||!this.popup) return;
    	Ext.get(document.documentElement).un("mousedown", this.triggerBlur, this);
    	this.popup.moveTo(-10000,-10000);
        this.popup.dom.style.opacity=0;
    	//this.shadow.moveTo(-1000,-1000);
        this.isExpanded = false;
    },
    /**
     * 展开弹出面板
     */
    expand : function(){
        if(this.disabled || this.isExpanded||!this.popup) return;
        this.doExpand();
    },
    doExpand : function(){
        //TODO:对于某些行上的cb，如果是二级关联的情况下，会expand多次，导致多次绑定事件
        Ext.get(document.documentElement).un("mousedown", this.triggerBlur, this)
                                         .on("mousedown", this.triggerBlur, this);
        this.syncPopup();
        this.isExpanded = true;
    },
    syncPopup : function(){
        var sl = document[Ext.isStrict&&!Ext.isWebKit?'documentElement':'body'].scrollLeft,
            st = document[Ext.isStrict&&!Ext.isWebKit?'documentElement':'body'].scrollTop,
            xy = this.wrap.getXY(),_x = xy[0] - sl,_y = xy[1] - st,
            W=this.popup.getWidth(),H=this.popup.getHeight(),
            PH=this.wrap.getHeight(),PW=this.wrap.getWidth(),
            BH=$S.getViewportHeight()-3,BW=$S.getViewportWidth()-3,
            x=((_x+W)>BW?((BW-W)<0?_x:(BW-W)):_x)+sl,
            y=((_y+PH+H)>BH?((_y-H)<0?(_y+PH):(_y-H)):(_y+PH))+st;
        this.popup.moveTo(x,y);
        this.popup.dom.style.opacity=1;
    },
    onKeyDown: function(e){
        switch(e.keyCode){
            case 9://TAB
            case 13://ENTER
            case 27:
                if(this.isExpanded){
                    this.collapse();
                    this.focus();
                    e.stopEvent();
                }
                break;//ESC
            case 40:
                this.expand();//DOWN
        }
        $S.TriggerField.superclass.onKeyDown.call(this,e);
    },
    onTriggerClick : function(e){
    	if(!this.isExpanded){
    		this.expand();
            this.el.focus();
    	}else{
    		this.collapse();
    	}
    },
    onInputClick : function(e){
        this.onTriggerClick(e);
    }
});